package cn.cs.service.impl;

import cn.cs.client.RepairBbsClient;
import cn.cs.common.*;
import cn.cs.pojo.RepairUser;
import cn.cs.service.RepairBbsService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Service
public class RepairBbsServiceImpl implements RepairBbsService {

    @Resource
    private RepairBbsClient repairBbsClient;

    public Page<Map<String, Object>> queryRepairBbsPageByMap(Map<String, Object> param, Integer pageNo, Integer pageSize) throws Exception {
        Integer total = repairBbsClient.getRepairBbsCountByMap(param);
        pageNo = EmptyUtils.isEmpty(pageNo) ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = EmptyUtils.isEmpty(pageSize) ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<Map<String, Object>> repairBbsTypeList = repairBbsClient.getRepairBbsListByMap(param);
        page.setRows(repairBbsTypeList);
        return page;
    }

    @Override
    public Dto getBBSDetails(String id){
//        Map<String,Object> bbs = mapper.getBbsById(id);
        Map<String,Object> bbs = new HashMap<>();
        List<Map<String,Object>> images = repairBbsClient.getBbsImages(id);
        if(EmptyUtils.isNotEmpty(images)){
            bbs.put("images",images);
        }

        List<Map<String,Object>> replys = repairBbsClient.getReplyTree(id);
        if(EmptyUtils.isNotEmpty(replys)){
            bbs.put("replys",replys);
        }
        return DtoUtil.returnDataSuccess(bbs);
    }

    /**
     * 官方发帖
     * @param request
     * @return
     * @throws Exception
     */
    public Dto releaseBbsAuthority(HttpServletRequest request) throws  Exception{
        Map<String,Object> bbs = new HashMap<>();
        String id  = IdWorker.getId();
        bbs.put("id",id );
        bbs.put("create_time",DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss") );
        bbs.put("seqNum",0);
        bbs.put("belong_type", "1");
        bbs.put("title", request.getParameter("title"));
        bbs.put("content", request.getParameter("content"));
        bbs.put("rep_user_id", null);
        bbs.put("type", request.getParameter("type"));

        int n = repairBbsClient.insertBbsByMap(bbs);
        if(n>0){
            String imgs = request.getParameter("img");
            for(String img :imgs.split(",")){
                Map<String,Object> map = new HashMap<String, Object>();
                map.put("id",IdWorker.getId());
                map.put("bbs_id",id);
                map.put("image_url",img);
                repairBbsClient.insertBbsImg(map);
            }
            return  DtoUtil.returnSuccess("发布成功", "");
        }else{
            return  DtoUtil.returnFail("发布失败","002");
        }
    }


    /**
     * 官方回复帖子
     * @param request
     * @return
     * @throws Exception
     */
    public Dto replyBbsAuthority(HttpServletRequest request) throws Exception {
        Map<String,Object> reply =  new HashMap<>();
        String bbs_id = request.getParameter("bbs_id");
        String pid = request.getParameter("pid");
        String id  = IdWorker.getId();
        Map<String,Object> bbsMap = repairBbsClient.getBbsById(bbs_id);
        if(EmptyUtils.isEmpty(pid)){
            reply.put("pid",bbs_id);
            int  n = Integer.valueOf(bbsMap.get("seqNum").toString())+1;
            reply.put("seqer",bbs_id.substring(0,5)+String.valueOf(n)+"1");
            bbsMap.put("seqNum",n);
            repairBbsClient.updateBbs(bbsMap);
        }else{
            Map<String,Object> replyMap =  repairBbsClient.getReplyById(pid);
            reply.put("seqer",Integer.valueOf(replyMap.get("seqer").toString())+1);
            reply.put("pid",pid);
        }
        reply.put("id",id );
        reply.put("bbs_id",bbs_id );

        reply.put("content",request.getParameter("content") );
        reply.put("rep_user_id", null);
        reply.put("create_time", DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss") );
        int n = repairBbsClient.insertReply(reply);
        if(n>0){
            return DtoUtil.returnSuccess("回复成功", "");
        }else{
            return  DtoUtil.returnFail("插入失败","002");
        }
    }


    /**
     * 帖子设为推荐，取消推荐
     * @param request
     * @return
     * @throws Exception
     */
    public Dto updateBBSRecommend(HttpServletRequest request) throws Exception {
        Map<String,Object> reply =  new HashMap<>();
        String id = request.getParameter("id");
        String isRecommend = request.getParameter("isRecommend");//是否推荐 0不推荐 1推荐
        reply.put("id",id );
        reply.put("isRecommend",isRecommend );

        int n = repairBbsClient.updateBBSRecommend(reply);
        if(n>0){
            return DtoUtil.returnSuccess("操作成功", "");
        }else{
            return  DtoUtil.returnFail("操作失败","002");
        }
    }
}
